package br.com.jw.dao;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import br.com.jw.modelo.Cliente;
import android.content.Context;

public class ClienteDao {
	String[] result;
	Context context;
	int resultado = 0;
	boolean aux = false;

	public ClienteDao(Context context) {
		this.context = context;
	}

	public boolean ativarDesativarCliente(int cli_id, String cli_status) {

		try {
			Socket socket = new Socket("10.0.2.2", 7777);
			DataInputStream in = new DataInputStream(socket.getInputStream());
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			out.writeUTF("clientes");
			out.writeUTF("ativarDesativarCliente");
			out.writeInt(cli_id);
			out.writeUTF(cli_status);

			out.flush();
			aux = in.readBoolean();

			out.close();
			in.close();
			socket.close();

			return aux;
		} catch (Exception e) {
			e.printStackTrace();
			e.getMessage();
		}
		return aux;
	}

	public ArrayList<Cliente> getListaClientes() {
		try {
			Socket socket = new Socket("10.0.2.2", 7777);
			DataInputStream in = new DataInputStream(socket.getInputStream());
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			out.writeUTF("clientes");
			out.writeUTF("listarClientes");
			out.flush();
			result = in.readUTF().split(",");

			ArrayList<Cliente> listaClientes = new ArrayList<Cliente>();
			for (int i = 0; i < result.length; i += 5) {

				listaClientes.add(new Cliente(Integer.parseInt(result[i]),
						result[i + 1], result[i + 2], result[i + 3],
						result[i + 4]));
			}

			out.close();
			in.close();
			socket.close();
			return listaClientes;

		} catch (Exception e) {
			e.printStackTrace();
			// mostra os erros no log
			e.getMessage();
			return null;
		}
	}

	public int alterarCliente(Cliente cliente) {
		try {
			Socket socket = new Socket("10.0.2.2", 7777);
			DataInputStream in = new DataInputStream(socket.getInputStream());
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			out.writeUTF("clientes");
			out.writeUTF("alterarCliente");

			out.writeInt(cliente.getCli_id());
			out.writeUTF(cliente.getCli_nome());
			out.writeUTF(cliente.getCli_telefone());
			out.writeUTF(cliente.getCli_cnpj());

			out.flush();// envia os dados

			resultado = in.readInt();

			out.close();
			in.close();
			socket.close();
			return resultado;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultado;
	}

	public boolean existeCnpj(Cliente cliente) {

		try {
			Socket socket = new Socket("10.0.2.2", 7777);
			DataInputStream in = new DataInputStream(socket.getInputStream());
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			out.writeUTF("clientes");
			out.writeUTF("existeCnpj");

			out.writeUTF(cliente.getCli_cnpj());
			out.flush();// envia os dados

			aux = in.readBoolean();

			out.close();
			in.close();
			socket.close();

			return aux;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return aux;
	}

	public int salvarCliente(Cliente cliente) {
		try {
			Socket socket = new Socket("10.0.2.2", 7777);
			DataInputStream in = new DataInputStream(socket.getInputStream());
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			out.writeUTF("clientes");
			out.writeUTF("salvarCliente");

			out.writeUTF(cliente.getCli_nome());
			out.writeUTF(cliente.getCli_telefone());
			out.writeUTF(cliente.getCli_cnpj());
			out.writeUTF(cliente.getCli_status());
			out.flush();// envia os dados

			resultado = in.readInt();

			out.close();
			in.close();
			socket.close();
			return resultado;

		} catch (Exception e) {
			e.printStackTrace();
			// return false;
		}
		return resultado;
	}
}
